# -*- coding: utf-8 -*-
import random
import string
import json
total=1
level=0
def addAChildToitem(item):
	if item.has_key('children'):
		item['children'].append(createAnEndNode())
	else:
		item['children']=[]
		item['children'].append(createAnEndNode())
	return item
def createAnEndNode():
	global total
	global level
	total+=1	
	return {	
		'name':str(++total),'level':level
	}
def createRandomInOut(node,parent):
	if parent==None:#说明这是根节点
		inVol=random.uniform(100000, 1000000)
		outVol=random.uniform(100000,1000000)
		while outVol>=inVol:
			outVol=random.uniform(100000,1000000)
		node['in']=str(inVol)
		node['out']=str(outVol)
		for child in node['children']:
			createRandomInOut(child,node)
	else:		
		if node.has_key('children'):
			node['in']=float(parent['out'])/len(parent['children'])
			if random.randint(0,2)==1:
				node['in']=node['in']*0.99999
			else:
				node['in']=node['in']
			#print float(parent['out'])/len(parent['children']),node['in']
			node['out']=node['in']
			node['in']=str(node['in'])
			node['out']=str(node['out'])
			for child in node['children']:
				createRandomInOut(child,node)
		else:#端点，使用的是电表类型，电表类型没有输入，只有输出售电量
			node['out']=float(parent['out'])/len(parent['children'])*0.85
			node['out']=str(node['out'])
			node['in']='0'
			return
if __name__ == '__main__':
	data={'name':'0','level':level}
	level+=1
	for i in xrange(0,random.randint(17,20)):
		addAChildToitem(data)
	level+=1
	for j in xrange(0,len(data['children'])):
		for i in xrange(1,random.randint(17,20)):
			addAChildToitem(data['children'][j])
	level+=1
	for j in xrange(0,len(data['children'])):
		for i in xrange(0,len(data['children'][j]['children'])):
			for z in xrange(1,random.randint(17,20)):
				addAChildToitem(data['children'][j]['children'][i])
	level+=1
	for j in xrange(0,len(data['children'])):
		for i in xrange(0,len(data['children'][j]['children'])):
			for z in xrange(0,len(data['children'][j]['children'][i]['children'])):
				for k in xrange(1,random.randint(15,17)):
					addAChildToitem(data['children'][j]['children'][i]['children'][z])	
	# level+=1
	# for i in xrange(0,random.randint(30,30)):
	# 	addAChildToitem(data)
	# level+=1
	# for j in xrange(0,len(data['children'])):
	# 	for i in xrange(1,random.randint(20,20)):
	# 		addAChildToitem(data['children'][j])
	# level+=1
	# for j in xrange(0,len(data['children'])):
	# 	for i in xrange(0,len(data['children'][j]['children'])):
	# 		for z in xrange(1,random.randint(30,35)):
	# 			addAChildToitem(data['children'][j]['children'][i])
	# level+=1
	# for j in xrange(0,len(data['children'])):
	# 	for i in xrange(0,len(data['children'][j]['children'])):
	# 		for z in xrange(0,len(data['children'][j]['children'][i]['children'])):
	# 			for k in xrange(1,random.randint(35,40)):
	# 				addAChildToitem(data['children'][j]['children'][i]['children'][z])	
	createRandomInOut(data,None)
	print total
	data['totalnodelength']=total	
	file=open('data.json','w')
	file.write(json.dumps(data,sort_keys=True))
	file.close()